import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        redirect: Home,
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
    },
    {
        path: '/goods',
        name: 'goods',

        component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue'),
    },
    {
        path: '/free',
        name: 'free',

        component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue'),
    },
    {
        path: '/order',
        name: 'order',

        component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue'),
    },
    {
        path: '*',

        component: () => import(/* webpackChunkName: "about" */ '../views/Error.vue'),
    },
    {
        path: '/user',
        name: 'user',

        component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
        redirect: '/user/cart',
        children: [
            {
                path: 'cart',
                component: () => import('@/components/user/Cart.vue'),
            },
        ],
        //路由独享
        // beforeEnter(to, from, next) {
        //     let token = localStorage.getItem('x-auth-token');
        //     if (token) {
        //         //如果存在，正常进去
        //         next();
        //     } else {
        //         //不存在提示还没有登录  使用toast组件
        //         store.dispatch('showToast/asyncchangeIsShowToast', {
        //             msg: '你还没有登录',
        //             type: 'warning',
        //         });
        //     }
        // },
    },
    {
        path: '/details',
        name: 'details',

        component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue'),
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // }
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
    return originalPush.call(this, location).catch(err => {});
};
//全局守卫
//router.beforeEach((to, from, next) => {
//     // 如果你准备去 user页面
//     if (to.path == '/user') {
//         //需要判断本地是否有token
//         let token = localStorage.getItem('x-auth-token');
//         if (token) {
//             //如果存在，正常进去
//             next();
//         } else {
//             //不存在提示还没有登录  使用toast组件
//             store.dispatch('showToast/asyncchangeIsShowToast', {
//                 msg: '你还没有登录',
//                 type: 'warning',
//             });
//         }
//         return; //预防代码继续执行下一个next
//     }
//     next();
// });
export default router;
